package com.inspur.dgov.bsp.user.feign;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.inspur.dgov.bsp.user.entity.PubUser;
import com.inspur.dgov.bsp.user.entity.PubUserRole;
import com.inspur.dgov.bsp.user.service.PubUserRoleService;
import com.inspur.dgov.bsp.user.service.PubUserService;
import com.inspur.dgov.common.secure.dto.AuthUser;
import com.inspur.dgov.common.tool.api.R;
import com.inspur.dgov.common.tool.constant.AppConstant;
import com.inspur.dgov.common.tool.utils.SMUtil;
import com.inspur.dgov.common.tool.utils.UniqueIdGenerator;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.configuration.DatabaseConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 用户服务Feign实现类
 *
 * @Author chiyupei
 * @Date 19/7/1 下午6:58
 * @Version 1.0
 * @Description 用户服务Feign实现类
 */
@RestController
@Slf4j
public class UserFeign implements IUserFeign {

    @Autowired
    private PubUserService pubUserService;

    @Autowired
    private PubUserRoleService pubUserRoleService;

    /**
     * 注入数据库常量配置
     */
    @Resource(name="databaseConfiguration")
    public DatabaseConfiguration systemConstant;

    @Value("${tenant.system.roleid}")
    private String tenantSystemRoleId;

    /**
     * 获取用户信息
     *
     * @param account  账号
     * @param password 密码
     * @param tenantId 租户ID
     * @return
     */
    @Override
    public R<AuthUser> userInfo(String account, String password, Long tenantId) {
        AuthUser user = new AuthUser();
        QueryWrapper<PubUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(PubUser::getStatus, AppConstant.DB_STATUS_NORMAL)
                .eq(PubUser::getAccount, account)
                .eq(PubUser::getPassword, password)
                .eq(PubUser::getTenantId, tenantId);

        Map<String, Object> userInfo = this.pubUserService.getMap(queryWrapper);
        if (null != userInfo) {
            user.setUser(userInfo);
            user.setUserType("0");
            QueryWrapper<PubUserRole> roleWrapper = new QueryWrapper<>();
            roleWrapper.lambda().eq(PubUserRole::getUserId, userInfo.get("ID"));
            List<PubUserRole> roleList = this.pubUserRoleService.list(roleWrapper);
            List<String> roles = roleList.stream().map(item -> String.valueOf(item.getRoleId())).collect(Collectors.toList());
            user.setRoles(roles);
        }
        return R.data(user);
    }

    /**
     * 创建租户管理员用户
     * @param user
     * @return
     */
    @Override
    public R addTenantSystemUser(PubUser user) {
        //密码为空则设置为默认密码
        String defaultPass = this.systemConstant.getString("tenant.default.password");
        if (StrUtil.isEmpty(user.getPassword())) {
            user.setPassword(SMUtil.sm3Encrypt(defaultPass));
        }
        boolean ret = this.pubUserService.save(user);
        //创建用户角色关系
        PubUserRole userRole = new PubUserRole();
        userRole.setId(UniqueIdGenerator.generateId());
        userRole.setUserId(user.getId());
        userRole.setRoleId(Long.valueOf(this.tenantSystemRoleId));
        this.pubUserRoleService.save(userRole);
        return R.status(ret);
    }
}
